<?php
/*
 * Created on 16.02.2009
 *
 * by Tulika Evheniy @ voltimum
 */
     require 'Zend/Loader.php';

     class InitApplication
     {
         /**
          * Configuration Object
          *
          * @var Zend_Config
          */
         private $_config = null;

         /**
          * Run application
          *
          * @var array $config Configuration
          */
         public function run($config)
         {
             try {

                 $this->setLoader();

                 $this->setConfig($config);

                 $this->setView();

                 $this->setDbAdapter();

//				 $this->setLogger();

                 $router = $this->setRouter();

                 $front = Zend_Controller_Front::getInstance();

                 $front->setBaseUrl($this->_config->url->base)
                       ->throwexceptions(true)
                       ->setRouter($router)
//                       ->registerPlugin(new App_Controller_Plugin_FlashMessenger())
                       ;

                 Zend_Controller_Front::run($this->_config->path->controllers->toArray());
             }
             catch (Exception $e) {
                 App_System_Exception::catchException($e);
             }
         }

         /**
          * Set Loaders
          */
         public function setLoader()
         {
             Zend_Loader::registerAutoload();
         }

         /**
          * Set config
          *
          * @param array $config Settings
          */
         public function setConfig($config)
         {
             $config = new Zend_Config($config);
             $this->_config = $config;
             Zend_Registry::set('config', $config);
         }

         /**
          * Set View
          */
         public function setView()
         {
             Zend_Layout::startMvc(array(
                 'layoutPath' => $this->_config->path->layouts,
                 'layout' => 'main',
             ));

             $layout = Zend_Layout::getMvcInstance();

             $view = $layout->getView();

             $layout->setViewSuffix('phtml');

//             $view->baseUrl = $this->_config->url->base;
//			 $view->cssUrl = App_System_Path::get().$this->_config->url->css;
//			 $view->jsUrl = App_System_Path::get().$this->_config->url->js;
//			 $view->imgUrl = App_System_Path::get().$this->_config->url->img;

             $view->setBasePath($this->_config->path->views);
			 $view->addScriptPath($this->_config->path->layouts);
//			 $view->addHelperPath($this->_config->path->helpers, 'App_Helper');
             $layout->setView($view);
             $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
             $viewRenderer
                 ->setView($view)
                 ->setViewSuffix('phtml');

             Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
         }

         /**
          * Set Database Connection and put it to registry
          */
         public function setDbAdapter()
         {
             if($this->_config->db->active) {
             	$db = Zend_Db::factory($this->_config->db);

             	Zend_Db_Table_Abstract::setDefaultAdapter($db);

             	Zend_Registry::set('db', $db);
             }
         }

         /**
          * Set Routes
          */
         public function setRouter()
         {
             require($this->_config->path->config . 'routes.php');
             if (!($router instanceof Zend_Controller_Router_Abstract)) {
                 throw new Exception('Incorrect config file: routes');
             }
             return $router;
         }

//         public function setControllers()
//         {
//         	$front->setControllerDirectory(array(
//    			'default' => '../application/controllers',
//    			'blog'    => '../modules/blog/controllers',
//    			'news'    => '../modules/news/controllers',
//			));
//         }

//         public function setLogger()
//         {
//			$logger = new Zend_Log();
//			$writer = null;
//			if($this->_config->env == 'dev') {
//				$writer = new Zend_Log_Writer_Firebug();
//				$writer->setEnabled($this->_config->debug->on);
//			} else {
//				$writer = new Zend_Log_Writer_Null();
//			}
//			$logger->addWriter($writer);
//			Zend_Registry::set('logger',$logger);
//			require $this->_config->system . "Debug.php";
//         }
     }


?>
